
let gfx = cc.gfx;

cc.Class({
    extends: cc.Component,
    editor: {
        executeInEditMode: true
    },

    properties: {
        handles: cc.Node,
        vertexes: [cc.Vec3],
        _vecLength: 5,
        vecLength: {
            set: function (val) {
                this._vecLength = val;
                this.reset();
            },
            get: function () {
                return this.vertexes.length;
            }
        },
        _distance: 3,
        distance: {
            set: function (val) {
                this._distance = val;
                this.reset();
            },
            get: function () {
                return this._distance;
            }
        },
        _jsonData: "",
        input: {
            get: function () {
                return this._jsonData;
            },
            set: function (val) {
                this._jsonData = val;
            }
        },
        output: {
            get: function () {
                return this._jsonData;
            },
            set: function (val) {
                this._jsonData = val;
            }
        },
    },
    start() {
        this.reset();
    },
    reset() {

        var vfmt = new gfx.VertexFormat([
            { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 3 },
            // { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
        ]);

        let mesh = new cc.Mesh();
        mesh.init(vfmt, this._vecLength, true);
        this.mesh = mesh;
        var p = cc.v2(0, this._distance);
        var angleStep = 360 / (this._vecLength - 2);
        this.vertexes = [];
        this.vertexes.push(cc.v3(0, 0, 150));
        this.vertexes.push(cc.v3(0, 0, 0));
        for (let i = 2; i < this._vecLength; i++) {
            var sp = p.rotate(cc.misc.degreesToRadians(angleStep * (i - 1)));

            this.vertexes.push(cc.v3(sp.x, sp.y, 0));
        }
        var indices = [];
        for (let i = 2; i < this._vecLength; i++) {
            indices.push(0);
            indices.push(i);
            if (i + 1 < this._vecLength) {
                indices.push(i + 1);
            }
            else {
                indices.push(2);
            }
        }
        for (let i = 2; i < this._vecLength; i++) {
            indices.push(1);
            if (i + 1 < this._vecLength) {
                indices.push(i + 1);
            }
            else {
                indices.push(2);
            }
            indices.push(i);
        }
        console.log(indices);
        mesh.setIndices(indices);

        mesh.setVertices(gfx.ATTR_POSITION, this.vertexes);

        this.handles.destroyAllChildren();
        for (let i = 0; i < this.vertexes.length; i++) {
            var node = new cc.Node();
            node.is3DNode = true;
            var lb = node.addComponent(cc.Label);
            lb.fontSize = 20;
            lb.lineHeight = 20;
            lb.string = `${i}`;
            node.x = this.vertexes[i].x;
            node.y = this.vertexes[i].y;
            node.z = this.vertexes[i].z;
            this.handles.addChild(node);
        }

        // mesh.setVertices(gfx.ATTR_UV0, [
        //     cc.v2(0, 0), /*cc.v2(0.5, 0),*/ cc.v2(1, 0),
        //     // cc.v2(0, 0.5), cc.v2(0.5, 0.5), cc.v2(1, 0.5),
        //     cc.v2(0.5,0.5),  /*cc.v2(0.5, 1), */ //cc.v2(1, 1),
        // ]);
        // mesh.setIndices([
        //     0, 2, 1
        // ]);

        // mesh.setIndices([
        //     0, 1, 3, 1, 4, 3,
        //     1, 2, 4, 2, 5, 4,
        //     3, 4, 6, 4, 7, 6,
        //     4, 5, 7, 5, 8, 7
        // ]);

        let renderer = this.node.getComponent(cc.MeshRenderer);
        if (!renderer) {
            renderer = this.node.addComponent(cc.MeshRenderer);
        }
        renderer.mesh = mesh;
        this.mesh = mesh;
    },
    updateVec() {
        if (!this.mesh) {
            return;
        }
        for (let i = 0; i < this.vertexes.length; i++) {
            if (!cc.isValid(this.handles.children[i])) continue;
            this.vertexes[i].x = this.handles.children[i].x;
            this.vertexes[i].y = this.handles.children[i].y;
            this.vertexes[i].z = this.handles.children[i].z;
        }
        this.mesh.setVertices(gfx.ATTR_POSITION, this.vertexes);

        this.output = JSON.stringify(this.vertexes);
    },

    update(dt) {
        // if (CC_EDITOR) return;
        this.updateVec();
    },
});
